package com.lonely.fx.combo_box;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;
import javafx.util.StringConverter;

/**
 * @date: 2022-07-18 12:34
 * @author: ztkj-hzb
 * @description: 定制化下拉项样式
 **/
public class 定制化下拉项样式效果演示 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        HBox hBox = new HBox();
        ComboBox<User> comboBox = new ComboBox<>();
        comboBox.setPrefWidth(250);

        User user1 = new User().setName("小A").setAge(10);
        User user2 = new User().setName("小B").setAge(20);
        User user3 = new User().setName("小C").setAge(30);


        comboBox.setConverter(new StringConverter<User>() {

            //注意，这里其实理解成后台数据处理效果
            @Override
            public String toString(User object) {
                if (object == null) {
                    return null;
                }
                return String.format("convert处理：%s-%s", object.getName(), object.getAge());
            }

            @Override
            public User fromString(String string) {
                return null;
            }
        });


        //这里理解成下拉展示样式定制化效果
        //为什么有些时候，我们没有设置cellFactory时候，能展示出convert效果呢，是因为fx内部设置了如果cellFactory为空，则展示样式取convert.toString(T)方法
        comboBox.setCellFactory(new Callback<ListView<User>, ListCell<User>>() {
            @Override
            public ListCell<User> call(ListView<User> param) {
                ListCell<User> listCell = new ListCell<User>() {
                    /**
                     * @expert
                     */
                    @Override
                    protected void updateItem(User item, boolean empty) {
                        //注意：改行代码不能删除
                        super.updateItem(item, empty);

                        if (empty || item == null) {
                            return;
                        }

                        //这里模拟javafx内部实现，当我们没有覆盖该方法时候，如何展示效果同convert处理效果一致得
                        //this.setGraphic(new Label(comboBox.getConverter().toString(item)));

                        HBox box = new HBox();
                        box.getChildren().add(new Label(String.format("cellFactory处理： %s-%s", item.getName(), item.getAge())));
                        box.setStyle("-fx-background-color: pink");
                        this.setGraphic(box);
                    }
                };
                return listCell;
            }
        });


        comboBox.getItems().addAll(user1, user2, user3);
        hBox.getChildren().addAll(comboBox);
        primaryStage.setScene(new Scene(hBox));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();


    }


    private class User {
        private String name;
        private Integer age;

        public String getName() {
            return name;
        }

        public User setName(String name) {
            this.name = name;
            return this;
        }

        public Integer getAge() {
            return age;
        }

        public User setAge(Integer age) {
            this.age = age;
            return this;
        }
    }


}
